package tgc.edu.examxy.web.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.examxy.entity.Calendar;
import tgc.edu.examxy.entity.Checkingin;
import tgc.edu.examxy.entity.CurriculumPlan;
import tgc.edu.examxy.entity.Student;
import tgc.edu.examxy.security.User2;
import tgc.edu.examxy.service.CalendarService;
import tgc.edu.examxy.service.CheckinginService;
import tgc.edu.examxy.service.CurriculumPlanService;
import tgc.edu.examxy.service.StudentService;

@Controller
@RequestMapping(value = "/calendar")
public class CalendarController { 
	@Autowired
	private CalendarService service;
	@Autowired
	private CheckinginService checkinginService ;
	@Autowired
	private StudentService studentservice;
	@Autowired
	private CurriculumPlanService currplanservice;
	@RequestMapping(value= "/manage")
	private String manage(ModelMap map) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
		String dfs = df.format(new Date());
		List<Calendar> Calendar = service.findByDatayear(dfs);
		if(Calendar.size()==0) {
			service.automatic();
		}
		return "calendar/manage";
	}
	@RequestMapping("/tolists")
	@ResponseBody
	private Object pages(String date) {
		SecurityContext ctx = SecurityContextHolder.getContext();
		Authentication auth = ctx.getAuthentication();
		User2 user = (User2) auth.getPrincipal();
		Student students = studentservice.findByUsername(user.getUsername());
		//获取当时月的时间
		if(date==""||date==null) {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
			date = df.format(new Date());
		}
		List<Calendar> Calendar = service.findByDatayear(date);
		
		SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd"); 
		String currenttime = dateFormat.format(new Date());
		
		//待处理，查看用户的签到情况
		for (int i = 0; i < Calendar.size(); i++) {
			try {
				//判断时间
				if(dateFormat.parse(Calendar.get(i).getData()).getTime()>dateFormat.parse(currenttime).getTime()) {
					Calendar.get(i).setIsqd(4);
				}else {
					/** 判断今天是否有课 **/
					List<CurriculumPlan> currplan = currplanservice.findByTbClassIdAndStartdata(students.getTbClass().getId(),Calendar.get(i).getData());
					if(currplan.size()<1) {
						Calendar.get(i).setIsqd(3);
					}else {
						List<Checkingin> cStudent = checkinginService. findByStudentAndDatesAndIsReach(students,Calendar.get(i).getData(),null);
						if(cStudent.size()==0) {
							List<Checkingin> tudent = checkinginService. findByStudentAndDates(students,Calendar.get(i).getData());
							if(tudent.size()<currplan.size()) {
								if(tudent.size()==0) {
									Calendar.get(i).setIsqd(0);
								}else {
									Calendar.get(i).setIsqd(1);
								}
							}else {
								Calendar.get(i).setIsqd(2);
							}
							
						}else if(cStudent.size()<currplan.size()&&cStudent.size()!=0){
							//有记录，就要查询当天是是否需要签到
							Calendar.get(i).setIsqd(1);
						}else if(cStudent.size()==currplan.size()) {
							//未签到
							Calendar.get(i).setIsqd(0);
						}
					}
					
				}
			} catch (ParseException e) {
				e.printStackTrace();
			}
			
		}
		return Calendar;
	}
	
	@RequestMapping(value="/signin")
	private String signin(String data,ModelMap map) {
		
		SecurityContext ctx = SecurityContextHolder.getContext();
		Authentication auth = ctx.getAuthentication();
		User2 user = (User2) auth.getPrincipal();
		Student students = studentservice.findByUsername(user.getUsername());
		List<CurriculumPlan> currplan = currplanservice.findByTbClassIdAndStartdata(students.getTbClass().getId(),data);
		for (CurriculumPlan c : currplan) {
			Checkingin che = checkinginService.findByStudentAndCurriculumPlanAndDatesAndBjuuid(students,c.getId(),data,students.getTbClass().getId().toString());
			String sum = che==null?"":che.getIsReach();
			if("".equals(sum)) {
				c.setIdqiandao("未签到");
			}else if("正常".equals(sum)){
				c.setIdqiandao("签到");
			}else {
				c.setIdqiandao(che.getIsReach());
			}
		}
		map.put("currplan", currplan);
		return "calendar/signin";
	}
}
